﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

namespace linkouqs.Gamekit
{
    public class Inventory : BasePanel
    {
        [HideInInspector]
        public Slot[] slotList;
        private CanvasGroup canvasGroup;


        protected Text coinText;
        public virtual void Start()
        {
            slotList = transform.Find("SlotPanel").GetComponentsInChildren<Slot>();
            coinText = transform.Find("CoinText").GetComponent<Text>();
        }

        public bool StoreItem(int id)
        {
            Item item = InventoryManager.Instance.GetItemById(id);
            return StoreItem(item);
        }

        public bool StoreItem(Item item)
        {
            if(item==null)
            {
                Debug.LogWarning("要存储的物品id不存在");
                return false;
            }

            if(item.Capacity==1)
            {
                Slot slot = FindEmptySlot();
                if(slot==null)
                {
                    Debug.LogWarning("没有空的物品槽");
                    return false;
                }
                else
                {
                    slot.StoreItem(item);//把物品存储到这个空的物品槽里面
                }

            }
            else
            {
                Slot slot = FindSameIdSlot(item);
                if (slot != null)
                {
                    slot.StoreItem(item);
                }
                else
                {
                    Slot emptySlot = FindEmptySlot();
                    if (emptySlot != null)
                    {
                        emptySlot.StoreItem(item);
                    }
                    else
                    {
                        Debug.LogWarning("没有空的物品槽");
                        return false;
                    }
                }

            }
            return true;
        }

        private Slot FindEmptySlot()
        {
            foreach(Slot slot in slotList)
            {
                if(slot.transform.childCount==0|| slot.transform.GetChild(0).name == "Price")
                {
                    return slot;
                }
            }
            return null;
        }

        private Slot FindSameIdSlot(Item item)
        {
            foreach(Slot slot in slotList)
            {
                if(slot.transform.childCount >= 1 && slot.transform.GetChild(0).name != "Price" && slot.GetItemId()==item.ID&&slot.IsFilled()==false)
                {
                    return slot;
                }
            }
            return null;
        }


        public override void OnEnter()
        {
            base.OnEnter();
         
            if (canvasGroup == null) canvasGroup = GetComponent<CanvasGroup>();
            canvasGroup.alpha = 1;
            canvasGroup.blocksRaycasts = true;

            Vector3 temp = transform.localPosition;
            temp.x = 800;
            transform.localPosition = temp;
            transform.DOLocalMoveX(0, 0.5f);


            //初始化金币
            if (coinText == null)
            {
                coinText = transform.Find("CoinText").GetComponent<Text>();
            }
            coinText.text = InventoryManager.Instance.CoinAmout.ToString();
        }

        public override void OnExit()
        {
            base.OnExit();
            
            canvasGroup.blocksRaycasts = false;

            transform.DOLocalMoveX(800, 0.5f).OnComplete(() => canvasGroup.alpha = 0);
        }

        public override void OnPause()
        {
            canvasGroup.blocksRaycasts = false;
        }

        public override void OnResume()
        {
            canvasGroup.blocksRaycasts = true;
        }

     

    }
}
